package org.jeecg.modules.dock.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.dock.entity.DockDataReceiveRecord;
import org.jeecg.modules.dock.entity.log.InterFaceLogQueryVO;
import org.jeecg.modules.dock.entity.vo.AbnormalLogVO;
import org.jeecg.modules.dock.service.IDockDataReceiveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 多系统交互数据接收记录
 * @Author: jeecg-boot
 * @Date: 2023-09-12
 * @Version: V1.0
 */
@Api(tags = "多系统交互数据接收记录")
@RestController
@RequestMapping("/receive/dockDataReceiveRecord")
@Slf4j
public class DockDataReceiveRecordController extends JeecgController<DockDataReceiveRecord, IDockDataReceiveRecordService> {
    @Autowired
    private IDockDataReceiveRecordService dockDataReceiveRecordService;

    /**
     * 分页列表查询
     *
     * @param dockDataReceiveRecord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "多系统交互数据接收记录-分页列表查询")
    @ApiOperation(value = "多系统交互数据接收记录-分页列表查询", notes = "多系统交互数据接收记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<DockDataReceiveRecord>> queryPageList(DockDataReceiveRecord dockDataReceiveRecord,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<DockDataReceiveRecord> queryWrapper = QueryGenerator.initQueryWrapper(dockDataReceiveRecord, req.getParameterMap());
        Page<DockDataReceiveRecord> page = new Page<DockDataReceiveRecord>(pageNo, pageSize);
        IPage<DockDataReceiveRecord> pageList = dockDataReceiveRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 异常日志-分页列表查询
     *
     * @param vo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return org.jeecg.common.api.vo.Result<com.baomidou.mybatisplus.core.metadata.IPage < org.jeecg.modules.dock.entity.DockDataReceiveRecord>>
     * @author hansen
     * @since 2023/11/30 15:09
     */
    @ApiOperation(value = "异常日志-分页列表查询", notes = "异常日志-分页列表查询")
    @GetMapping(value = "/errorList")
    public Result<IPage<AbnormalLogVO>> errorList(InterFaceLogQueryVO vo,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        LambdaQueryWrapper<DockDataReceiveRecord> queryWrapper = new LambdaQueryWrapper<>();
        Page<DockDataReceiveRecord> page = new Page<DockDataReceiveRecord>(pageNo, pageSize);
        List<String> strings = Arrays.asList("FAILURE", "E");
        // 拼接查询条件
        if (StrUtil.isNotBlank(vo.getSource())){
            queryWrapper.eq(DockDataReceiveRecord::getSource,vo.getSource());
        }
        if (StrUtil.isNotBlank(vo.getBusinessDocument())){
            queryWrapper.eq(DockDataReceiveRecord::getBusinessDocument,vo.getBusinessDocument());
        }
        if (StrUtil.isNotBlank(vo.getDocumentCode())){
            queryWrapper.like(DockDataReceiveRecord::getDocumentCode,vo.getDocumentCode());
        }

        if (StrUtil.isNotBlank(vo.getKeyWard())) {
            queryWrapper.like(DockDataReceiveRecord::getData, vo.getKeyWard());
        }
        if (StrUtil.isNotBlank(vo.getCreateTime_begin())) {
            queryWrapper.ge(DockDataReceiveRecord::getCreateTime, DateUtil.parseDate(vo.getCreateTime_begin()));
        }
        if (StrUtil.isNotBlank(vo.getCreateTime_end())) {
            queryWrapper.ge(DockDataReceiveRecord::getCreateTime, DateUtil.parseDate(vo.getCreateTime_end()));
        }
        queryWrapper.in(DockDataReceiveRecord::getStatus, strings);
        queryWrapper.orderByDesc(DockDataReceiveRecord::getCreateTime);
        IPage<DockDataReceiveRecord> pageList = dockDataReceiveRecordService.page(page, queryWrapper);
        IPage<AbnormalLogVO> res = new Page<>();
        List<AbnormalLogVO> resList = new ArrayList<>();
        pageList.getRecords().forEach(one->{
            AbnormalLogVO abnormalLogVO = new AbnormalLogVO();
            abnormalLogVO.setId(one.getId());
            abnormalLogVO.setLogNumber(one.getUuid());
            abnormalLogVO.setSystemModule(one.getBusinessDocument());
            abnormalLogVO.setOperationType(one.getNodeName());
            abnormalLogVO.setRequestMethod("POST");
            abnormalLogVO.setOperatingPersonnel(one.getSource()+"人员");
            abnormalLogVO.setOperatingAddress(one.getInterfaceName());
            abnormalLogVO.setOperatingStatus("失败");
            abnormalLogVO.setReceiveTime(one.getCreateTime());
            resList.add(abnormalLogVO);
        });
        res.setRecords(resList);
        res.setCurrent(pageList.getCurrent());
        res.setSize(pageList.getSize());
        res.setTotal(pageList.getTotal());
        res.setPages(pageList.getPages());
        return Result.OK(res);
    }

    /**
     * 添加
     *
     * @param dockDataReceiveRecord
     * @return
     */
    @AutoLog(value = "多系统交互数据接收记录-添加")
    @ApiOperation(value = "多系统交互数据接收记录-添加", notes = "多系统交互数据接收记录-添加")
    @RequiresPermissions("receive:dock_data_receive_record:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody DockDataReceiveRecord dockDataReceiveRecord) {
        dockDataReceiveRecordService.save(dockDataReceiveRecord);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param dockDataReceiveRecord
     * @return
     */
    @AutoLog(value = "多系统交互数据接收记录-编辑")
    @ApiOperation(value = "多系统交互数据接收记录-编辑", notes = "多系统交互数据接收记录-编辑")
    @RequiresPermissions("receive:dock_data_receive_record:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody DockDataReceiveRecord dockDataReceiveRecord) {
        dockDataReceiveRecordService.updateById(dockDataReceiveRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "多系统交互数据接收记录-通过id删除")
    @ApiOperation(value = "多系统交互数据接收记录-通过id删除", notes = "多系统交互数据接收记录-通过id删除")
    @RequiresPermissions("receive:dock_data_receive_record:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        dockDataReceiveRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "多系统交互数据接收记录-批量删除")
    @ApiOperation(value = "多系统交互数据接收记录-批量删除", notes = "多系统交互数据接收记录-批量删除")
    @RequiresPermissions("receive:dock_data_receive_record:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.dockDataReceiveRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "多系统交互数据接收记录-通过id查询")
    @ApiOperation(value = "多系统交互数据接收记录-通过id查询", notes = "多系统交互数据接收记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<DockDataReceiveRecord> queryById(@RequestParam(name = "id", required = true) String id) {
        DockDataReceiveRecord dockDataReceiveRecord = dockDataReceiveRecordService.getById(id);
        if (dockDataReceiveRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dockDataReceiveRecord);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param dockDataReceiveRecord
     */
    @RequiresPermissions("receive:dock_data_receive_record:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DockDataReceiveRecord dockDataReceiveRecord) {
        return super.exportXls(request, dockDataReceiveRecord, DockDataReceiveRecord.class, "多系统交互数据接收记录");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("receive:dock_data_receive_record:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DockDataReceiveRecord.class);
    }

    @ApiOperation(value = "多系统交互数据接收记录-分页列表查询", notes = "多系统交互数据接收记录-分页列表查询")
    @GetMapping(value = "/queryInterfacePageList")
    public Result<IPage<DockDataReceiveRecord>> queryInterfacePageList(InterFaceLogQueryVO vo,
                                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                HttpServletRequest req) {
        LambdaQueryWrapper<DockDataReceiveRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(vo.getKeyWard())) {
            queryWrapper.like(DockDataReceiveRecord::getData, vo.getKeyWard());
        }
        if (StrUtil.isNotBlank(vo.getCreateTime_begin())) {
            queryWrapper.ge(DockDataReceiveRecord::getCreateTime, DateUtil.parseDate(vo.getCreateTime_begin()));
        }
        if (StrUtil.isNotBlank(vo.getCreateTime_end())) {
            queryWrapper.le(DockDataReceiveRecord::getCreateTime, DateUtil.parseDate(vo.getCreateTime_end()));
        }
        queryWrapper.orderByDesc(DockDataReceiveRecord::getCreateTime);
        Page<DockDataReceiveRecord> page = new Page<DockDataReceiveRecord>(pageNo, pageSize);
        IPage<DockDataReceiveRecord> pageList = dockDataReceiveRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

}
